package com.roamer.audition.web.controller;

import com.roamer.audition.common.request.AuditionRecordSimpleDTO;
import com.roamer.audition.common.request.CompanyDTO;
import com.roamer.audition.common.request.CompanyPageQuery;
import com.roamer.audition.common.request.CompanyQuery;
import com.roamer.audition.common.response.AuditionRecordVO;
import com.roamer.audition.common.response.CompanyVO;
import com.roamer.audition.common.service.AuditionRecordService;
import com.roamer.audition.common.service.CompanyService;
import com.roamer.audition.web.export.CompanyExcelService;
import com.roamer.validator.gruop.Add;
import com.roamer.validator.gruop.Update;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公司 API
 *
 * @author roamer
 * @version V1.0
 * @date 2019-08-21 00:47
 */
@Api(tags = "公司 API")
@Slf4j
@RestController
@RequestMapping("/companies")
public class CompanyController {

    private final CompanyService companyService;

    private final AuditionRecordService auditionRecordService;

    private final CompanyExcelService companyExcelService;

    public CompanyController(
            CompanyService companyService,
            AuditionRecordService auditionRecordService,
            CompanyExcelService companyExcelService) {
        this.companyService = companyService;
        this.auditionRecordService = auditionRecordService;
        this.companyExcelService = companyExcelService;
    }

    @ApiOperation(value = "新增公司", notes = "创建新的公司")
    @PostMapping(consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public CompanyVO createCompany(@Validated(Add.class) @RequestBody CompanyDTO companyDTO) {
        return companyService.createCompany(companyDTO);
    }

    @ApiOperation(value = "修改公司", notes = "修改原有公司")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", paramType = "path", value = "公司ID")})
    @PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public CompanyVO updateCompany(@PathVariable Long id, @Validated(Update.class) @RequestBody CompanyDTO companyDTO) {
        companyDTO.setId(id);
        return companyService.updateCompany(companyDTO);
    }

    @ApiOperation(value = "删除公司", notes = "删除公司，同时删除关联面试计划")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", paramType = "path", value = "公司ID")})
    @DeleteMapping(value = "/{id}")
    public void deleteCompany(@PathVariable Long id) {
        companyService.deleteCompany(id);
    }

    @ApiOperation(value = "公司详情", notes = "根据ID查询公司")
    @ApiImplicitParams(@ApiImplicitParam(name = "id", paramType = "path", value = "公司ID"))
    @GetMapping(value = "/{id}")
    public CompanyVO getCompany(@PathVariable Long id) {
        return companyService.getCompany(id);
    }

    @ApiOperation(value = "公司分页列表", notes = "分页查询公司列表")
    @GetMapping(value = "/page")
    public Page<CompanyVO> getCompanyPage(@Validated CompanyPageQuery companyPageQuery) {
        return companyService.getCompanyPage(companyPageQuery);
    }

    @ApiOperation(value = "公司列表", notes = "查询公司列表")
    @GetMapping
    public List<CompanyVO> getCompanyList(@Validated CompanyQuery companyQuery) {
        return companyService.getCompanyList(companyQuery);
    }

    @ApiOperation(value = "新增面试计划", notes = "创建新的面试计划")
    @ApiImplicitParams(@ApiImplicitParam(name = "companyId", paramType = "path", value = "公司ID"))
    @PostMapping(value = "/{companyId}/auditionRecords", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public AuditionRecordVO createAuditionRecord(
            @PathVariable Long companyId,
            @Validated(Add.class) @RequestBody AuditionRecordSimpleDTO auditionRecordSimpleDTO) {
        auditionRecordSimpleDTO.setCompanyId(companyId);
        return auditionRecordService.createAuditionRecord(auditionRecordSimpleDTO);
    }

    @ApiOperation(value = "导出公司列表", notes = "导出Excel")
    @GetMapping(value = "/export/excel")
    public void exportExcel() {
        companyExcelService.exportExcel();
    }
}
